登录 白背景

5922. 统计出现过一次的公共字符串

https://leetcode-cn.com/problems/count-common-words-with-one-occurrence/

  • 提交时间:2021-11-28 05:17:26
  • 执行用时:8 ms, 在所有 Go 提交中击败了100.00%的用户
  • 内存消耗:6.6 MB, 在所有 Go 提交中击败了100.00%的用户
  • 通过测试用例:60 / 60
func countWords(words1 []string, words2 []string) (ans int) {
    wordMap1, wordMap2 := map[string]int{}, map[string]int{}
    for _, word := range words1 {
        wordMap1[word]++
    }
    for _, word := range words2 {
        wordMap2[word]++
    }
    for word, count := range wordMap1 {
        if count != 1 {
            continue
        }
        if count2, ok := wordMap2[word]; ok && count2 == 1 {
            ans++
        }
    }
    return
}